package com.example.test15middleware.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class JsonSerializeUtil {
    private static String SERIALIZE_FAIL = "%s 序列化失败";
    private static String RE_SERIALIZE_FAIL = "%s 反序列化失败";
    private static ConcurrentHashMap objWriterCache = new ConcurrentHashMap();
    private static ConcurrentHashMap objWriterCacheNoType = new ConcurrentHashMap();
    private static ConcurrentHashMap objWriterCacheNoNull = new ConcurrentHashMap();
    private static ObjectMapper mapper = new ObjectMapper();
    private static ObjectMapper mapperNoType = new ObjectMapper();
    private static ObjectMapper mapperNoNull = new ObjectMapper();

    private JsonSerializeUtil() {
        throw new IllegalStateException("Utility class");
    }

    private static ObjectWriter getObjWriter(Class<?> serializationView) {
        if (objWriterCache.get(serializationView) != null) {
            return (ObjectWriter)objWriterCache.get(serializationView);
        } else {
            ObjectWriter temp = mapper.writerWithView(serializationView);
            objWriterCache.put(serializationView, temp);
            return temp;
        }
    }

    private static ObjectWriter getObjWriterNoType(Class<?> serializationView) {
        if (objWriterCacheNoType.get(serializationView) != null) {
            return (ObjectWriter)objWriterCacheNoType.get(serializationView);
        } else {
            ObjectWriter temp = mapperNoType.writerWithView(serializationView);
            objWriterCacheNoType.put(serializationView, temp);
            return temp;
        }
    }

    private static ObjectWriter getObjWriterNoNull(Class<?> serializationView) {
        if (objWriterCacheNoNull.get(serializationView) != null) {
            return (ObjectWriter)objWriterCacheNoNull.get(serializationView);
        } else {
            mapperNoNull.setSerializationInclusion(Include.NON_NULL);
            ObjectWriter temp = mapperNoNull.writerWithView(serializationView);
            objWriterCacheNoNull.put(serializationView, temp);
            return temp;
        }
    }

    public static String jsonSerializer(Object originalObject) {
        try {
            mapper.enableDefaultTyping(DefaultTyping.NON_FINAL);
            ObjectWriter objectWriter = getObjWriter(originalObject.getClass());
            String json = objectWriter.writeValueAsString(originalObject);
            return json;
        } catch (Exception var3) {
            throw new JsonSerializeUtil.SerializerException(String.format(SERIALIZE_FAIL, originalObject.getClass().getName()), var3);
        }
    }

    public static <T> T jsonReSerializer(String jsonStr, Class<T> calzz) {
        try {
            mapper.enableDefaultTyping(DefaultTyping.NON_FINAL);
            ObjectReader reader = mapper.reader(calzz);
            T object = reader.readValue(jsonStr);
            return object;
        } catch (Exception var4) {
            throw new JsonSerializeUtil.SerializerException(String.format(RE_SERIALIZE_FAIL, jsonStr), var4);
        }
    }

    public static String jsonSerializerNoType(Object originalObject) {
        try {
            ObjectWriter objectWriter = getObjWriterNoType(originalObject.getClass());
            String json = objectWriter.writeValueAsString(originalObject);
            return json;
        } catch (Exception var3) {
            throw new JsonSerializeUtil.SerializerException(String.format(SERIALIZE_FAIL, originalObject.getClass().getName()), var3);
        }
    }

    public static <T> T jsonReSerializerNoType(String jsonStr, Class<T> calzz) {
        try {
            ObjectReader reader = mapperNoType.reader(calzz);
            T object = reader.readValue(jsonStr);
            return object;
        } catch (Exception var4) {
            throw new JsonSerializeUtil.SerializerException(String.format(RE_SERIALIZE_FAIL, jsonStr), var4);
        }
    }

    public static String jsonSerializerNoNull(Object originalObject) {
        try {
            ObjectWriter objectWriter = getObjWriterNoNull(originalObject.getClass());
            String json = objectWriter.writeValueAsString(originalObject);
            return json;
        } catch (Exception var3) {
            throw new JsonSerializeUtil.SerializerException(String.format(RE_SERIALIZE_FAIL, originalObject.getClass().getName()), var3);
        }
    }

    public static <T> T jsonReSerializerNoNull(String jsonStr, Class<T> calzz) {
        try {
            ObjectReader reader = mapperNoNull.reader(calzz);
            T object = reader.readValue(jsonStr);
            return object;
        } catch (Exception var4) {
            throw new JsonSerializeUtil.SerializerException(String.format(RE_SERIALIZE_FAIL, jsonStr), var4);
        }
    }

    public static <T> List<T> jsonReSerializerTypeForList(String jsonStr, Class<T> calzz) {
        try {
            JavaType javaType = mapperNoType.getTypeFactory().constructCollectionType(List.class, calzz);
            List<T> list2 = (List)mapperNoType.readValue(jsonStr, javaType);
            return list2;
        } catch (Exception var4) {
            throw new JsonSerializeUtil.SerializerException(String.format(RE_SERIALIZE_FAIL, jsonStr), var4);
        }
    }

    static {
        mapperNoType.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    private static class SerializerException extends RuntimeException {
        public SerializerException(String format, Exception e) {
        }
    }
}
